//
//  ST_PayPasswordView.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/3/2.
//

#import "ST_PayPasswordView.h"
//V
#import "HNSH_PayPasswordView.h"

#import "HNSH_SafeHomeViewController.h"

typedef void(^SureBlock)(NSString * password);
typedef void(^CancelBlock)(void);

@interface ST_PayPasswordView ()<UIGestureRecognizerDelegate, PayPasswordViewDelegate>
/** 取消按钮 */
@property (nonatomic, strong)UIButton * cancelBtn;
/** 白色背景 */
@property (nonatomic, strong)UIView * bgView;
/** 标题 */
@property (nonatomic, strong)UILabel * titleLabel;
/** 金额 */
@property (nonatomic, strong)UIView * segView;
/** 输入框 */
@property (nonatomic, strong)HNSH_PayPasswordView * passwordView;
/** SureBlock */
@property (nonatomic, copy)SureBlock sureBlock;
/** CancelBlock */
@property (nonatomic, copy)CancelBlock cancelBlock;
@end

@implementation ST_PayPasswordView

//MRAK: Cycle Methods
+ (ST_PayPasswordView *)showPasswordViewWithSureBlock:(void(^)(NSString * password))sureBlock cancelBlock:(void(^)(void))cancelBlock{
    if (![ST_UserManager shareInstance].user.pay_password_status) {
        [HNSH_AlertView showTitle:@"请设置支付密码"
                              des:@""
                      buttonTitle:@"确定"
                      buttonColor:[UIColor appGreenColor]
                      buttonClick:^{
            HNSH_SafeHomeViewController *vc = [[HNSH_SafeHomeViewController alloc]init];
            vc.style = HNSH_SafeHomeStyle_ChangePay;
            [[UIViewControllerHelper findCurrentShowingViewController].navigationController pushViewController:vc animated:YES];
        }];
        return nil;
    }
    
    ST_PayPasswordView * view = [[ST_PayPasswordView alloc] initWithFrame:kSTWindow.bounds];
    view.sureBlock = sureBlock;
    view.cancelBlock = cancelBlock;
    [kSTWindow addSubview:view];
    return view;
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setDefault];
        [self addObserver];
        [self addAllViews];
        [self addAllLayout];
    }
    return self;
}

//MRAK: Myself Methods
- (void)addAllViews{
    [self addSubview:self.bgView];
    [self.bgView addSubview:self.cancelBtn];
    [self.bgView addSubview:self.titleLabel];
    [self.bgView addSubview:self.segView];
    [self.bgView addSubview:self.passwordView];
}

- (void)addAllLayout{
    CGFloat top = (kScreenHeight - 178) / 2;
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(178);
        make.top.mas_equalTo(top);
    }];
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.width.height.mas_equalTo(50);
        make.top.mas_equalTo(0);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.cancelBtn.mas_right);
        make.right.mas_equalTo(-50);
        make.top.height.mas_equalTo(self.cancelBtn);
    }];
    [self.segView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(1);
        make.top.mas_equalTo(self.titleLabel.mas_bottom);
    }];
    [self.passwordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.width.mas_equalTo(50 * 6);
        make.top.mas_equalTo(self.segView.mas_bottom).offset(30);
        make.height.mas_equalTo(50);
    }];
}

- (void)setDefault{
    self.backgroundColor = RGBA(0, 0, 0, 0.5);
}

//MARK: 点击取消
- (void)cancelBtnAction:(UIButton *)sender{
    [self removeFromSuperview];
}

- (void)addObserver{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification *)notification{
    CGRect frame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat height = frame.size.height;
    [UIView animateWithDuration:[[[notification userInfo] objectForKey:@"UIKeyboardAnimationDurationUserInfoKey"] floatValue] animations:^{
        CGFloat top = kScreenHeight - height - 178;
        [self.bgView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(top);
        }];
    }];
}

- (void)keyboardWillHide:(NSNotification *)notification{
    [UIView animateWithDuration:[[[notification userInfo] objectForKey:@"UIKeyboardAnimationDurationUserInfoKey"] floatValue] animations:^{
        CGFloat top = (kScreenHeight - 178) / 2;
        [self.bgView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(top);
        }];
    }];
}

//MARK: Getter Methods
- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textColor = [UIColor colorWithHexString:@"#383C3F"];
        _titleLabel.font = [UIFont systemFontOfSize:16];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.text = @"请输入支付密码";
    }
    return _titleLabel;
}

- (UIView *)segView{
    if (!_segView) {
        _segView = [UIView new];
        _segView.backgroundColor = [UIColor colorWithHexString:@"#F4F4F4"];
    }
    return _segView;
}

- (HNSH_PayPasswordView *)passwordView{
    if (!_passwordView) {
        _passwordView = [[HNSH_PayPasswordView alloc] init];
        _passwordView.backgroundColor = [UIColor whiteColor];
        _passwordView.rectColor = [UIColor colorWithHexString:@"#E1E1E1"];
        _passwordView.passwordNumber = 6;
        _passwordView.squareSize = 50;
        _passwordView.delegate = self;
    }
    return _passwordView;
}

- (UIButton *)cancelBtn{
    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelBtn setImage:[UIImage imageNamed:@"base_icon_close"] forState:UIControlStateNormal];
        [_cancelBtn addTarget:self action:@selector(cancelBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

//MARK: PayPasswordViewDelegate Methods
/** 监听输入完成时 */
- (void)passwordCompleteInput:(HNSH_PayPasswordView *)password{
    if (self.sureBlock) {
        self.sureBlock(password.saveStore.copy);
    }
    [self removeFromSuperview];
}

@end
